<html>
<head><meta charset="utf-8"><title>Option&lt;Iterator&gt; iterator · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Option.3CIterator.3E.20iterator.html">Option&lt;Iterator&gt; iterator</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="208171965"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Option%3CIterator%3E%20iterator/near/208171965" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Option.3CIterator.3E.20iterator.html#208171965">(Aug 27 2020 at 02:09)</a>:</h4>
<p>Is there an implementation of an iterator over (a newtype around) <code>Option&lt;impl Iterator&gt;</code> in the standard library? I notice that <code>Fuse</code> has exactly this type but doesn't let you construct the <code>None</code> variant</p>



<a name="208172017"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Option%3CIterator%3E%20iterator/near/208172017" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Option.3CIterator.3E.20iterator.html#208172017">(Aug 27 2020 at 02:10)</a>:</h4>
<p>er, Fuse is not that type, or at least has different behavior than Option</p>



<a name="208172027"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Option%3CIterator%3E%20iterator/near/208172027" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Option.3CIterator.3E.20iterator.html#208172027">(Aug 27 2020 at 02:10)</a>:</h4>
<p>We can add a Fuse::empty() constructor if that'd be helpful, though</p>



<a name="208172272"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Option%3CIterator%3E%20iterator/near/208172272" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Option.3CIterator.3E.20iterator.html#208172272">(Aug 27 2020 at 02:17)</a>:</h4>
<p>I realize this is kind of an abuse of <code>Fuse</code>. If there is a better type with the same representation I'm all ears</p>



<a name="208172313"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Option%3CIterator%3E%20iterator/near/208172313" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Option.3CIterator.3E.20iterator.html#208172313">(Aug 27 2020 at 02:18)</a>:</h4>
<p>What are you using it for?</p>



<a name="208172319"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Option%3CIterator%3E%20iterator/near/208172319" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Option.3CIterator.3E.20iterator.html#208172319">(Aug 27 2020 at 02:18)</a>:</h4>
<p>I want to return either an iterator or empty</p>



<a name="208172321"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Option%3CIterator%3E%20iterator/near/208172321" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Option.3CIterator.3E.20iterator.html#208172321">(Aug 27 2020 at 02:18)</a>:</h4>
<p>But not an empty Iterator?</p>



<a name="208172331"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Option%3CIterator%3E%20iterator/near/208172331" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Option.3CIterator.3E.20iterator.html#208172331">(Aug 27 2020 at 02:19)</a>:</h4>
<p>I want something that yields elements of the underlying iterator, in the first case, and yields none immediately in the second case</p>



<a name="208172363"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Option%3CIterator%3E%20iterator/near/208172363" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Option.3CIterator.3E.20iterator.html#208172363">(Aug 27 2020 at 02:19)</a>:</h4>
<p>I can implement such a thing with a data representation that is <code>Option&lt;I&gt;</code> but I assume such a thing already exists</p>



<a name="208172366"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Option%3CIterator%3E%20iterator/near/208172366" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Option.3CIterator.3E.20iterator.html#208172366">(Aug 27 2020 at 02:19)</a>:</h4>
<p>You could do it with .filter I guess</p>



<a name="208172416"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Option%3CIterator%3E%20iterator/near/208172416" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Option.3CIterator.3E.20iterator.html#208172416">(Aug 27 2020 at 02:20)</a>:</h4>
<p>The current best contender I think is <code>flatten</code> on <code>Option::IntoIter</code></p>



<a name="208172422"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Option%3CIterator%3E%20iterator/near/208172422" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Option.3CIterator.3E.20iterator.html#208172422">(Aug 27 2020 at 02:20)</a>:</h4>
<p>but I think the data representation of that is somewhat larger than just <code>Option&lt;I&gt;</code></p>



<a name="208172435"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Option%3CIterator%3E%20iterator/near/208172435" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Option.3CIterator.3E.20iterator.html#208172435">(Aug 27 2020 at 02:21)</a>:</h4>
<p>Hm shouldn't be I think</p>



<a name="208172443"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Option%3CIterator%3E%20iterator/near/208172443" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Option.3CIterator.3E.20iterator.html#208172443">(Aug 27 2020 at 02:21)</a>:</h4>
<p>.filter will work though without the option</p>



<a name="208172497"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Option%3CIterator%3E%20iterator/near/208172497" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Option.3CIterator.3E.20iterator.html#208172497">(Aug 27 2020 at 02:22)</a>:</h4>
<p>I would not be opposed to adding a method to Fuse which, uh, lights the fuse so to speak too</p>



<a name="208172502"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Option%3CIterator%3E%20iterator/near/208172502" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Option.3CIterator.3E.20iterator.html#208172502">(Aug 27 2020 at 02:22)</a>:</h4>
<p>(forcing it to return None)</p>



<a name="208172562"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Option%3CIterator%3E%20iterator/near/208172562" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Option.3CIterator.3E.20iterator.html#208172562">(Aug 27 2020 at 02:24)</a>:</h4>
<p><code>flatten</code> has quite a lot of stuff in it, because it supports forward and reverse iteration and also fuses it's internal iterator</p>



<a name="208172578"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Option%3CIterator%3E%20iterator/near/208172578" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Option.3CIterator.3E.20iterator.html#208172578">(Aug 27 2020 at 02:25)</a>:</h4>
<p>How would <code>filter</code> work? In the none case I don't have an iterator to filter in the first place</p>



<a name="208172664"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Option%3CIterator%3E%20iterator/near/208172664" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Option.3CIterator.3E.20iterator.html#208172664">(Aug 27 2020 at 02:26)</a>:</h4>
<p>Oh, I guess I misunderstood</p>



<a name="208172669"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Option%3CIterator%3E%20iterator/near/208172669" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Option.3CIterator.3E.20iterator.html#208172669">(Aug 27 2020 at 02:26)</a>:</h4>
<p>Yeah flatten is probably your best bet</p>



<a name="208173308"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Option%3CIterator%3E%20iterator/near/208173308" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Option.3CIterator.3E.20iterator.html#208173308">(Aug 27 2020 at 02:44)</a>:</h4>
<p>Note that <code>Fuse</code> assumes it will never have <code>None</code> for a <code>FusedIterator</code></p>



<a name="208173334"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Option%3CIterator%3E%20iterator/near/208173334" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Option.3CIterator.3E.20iterator.html#208173334">(Aug 27 2020 at 02:45)</a>:</h4>
<p>You could use <code>Either&lt;I, Empty&gt;</code> in general, which I think will even have the same size as <code>Option&lt;I&gt;</code></p>



<a name="208173548"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Option%3CIterator%3E%20iterator/near/208173548" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Option.3CIterator.3E.20iterator.html#208173548">(Aug 27 2020 at 02:50)</a>:</h4>
<p>I noticed that; I'm now seeing if I can prove that this is UB in safe code:</p>
<div class="codehilite"><pre><span></span><code><span class="k">use</span><span class="w"> </span><span class="n">std</span>::<span class="n">iter</span>::<span class="n">FusedIterator</span><span class="p">;</span><span class="w"></span>

<span class="k">struct</span> <span class="nc">Liar</span><span class="p">(</span><span class="kt">bool</span><span class="p">);</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="nb">Iterator</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">Liar</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">type</span> <span class="nc">Item</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">next</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nb">Option</span><span class="o">&lt;</span><span class="p">()</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="n">std</span>::<span class="n">mem</span>::<span class="n">take</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="mi">0</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nb">None</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nb">Some</span><span class="p">(())</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
<span class="k">impl</span><span class="w"> </span><span class="n">FusedIterator</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">Liar</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">it</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Liar</span><span class="p">(</span><span class="kc">true</span><span class="p">).</span><span class="n">fuse</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="n">it</span><span class="p">.</span><span class="n">next</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;{:?}&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">it</span><span class="p">.</span><span class="n">next</span><span class="p">());</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p><code>miri</code> doesn't notice any issue, but it doesn't print <code>None</code>. I don't know enough about getting rust to show the relevant mir output to confirm</p>



<a name="208173630"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Option%3CIterator%3E%20iterator/near/208173630" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Option.3CIterator.3E.20iterator.html#208173630">(Aug 27 2020 at 02:52)</a>:</h4>
<p>Assuming I'm reading it correctly, either the <code>unreachable_unchecked</code> needs to be downgraded to <code>unreachable!</code> or <code>FusedIterator</code> should be an unsafe trait</p>



<a name="208173647"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Option%3CIterator%3E%20iterator/near/208173647" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Option.3CIterator.3E.20iterator.html#208173647">(Aug 27 2020 at 02:52)</a>:</h4>
<p>Why? You haven't demonstrated any memory unsafety.</p>



<a name="208173683"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Option%3CIterator%3E%20iterator/near/208173683" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Option.3CIterator.3E.20iterator.html#208173683">(Aug 27 2020 at 02:53)</a>:</h4>
<p>It's semantically wrong since your <code>FusedIterator</code> doesn't fuse itself, but it's still safe.</p>



<a name="208173815"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Option%3CIterator%3E%20iterator/near/208173815" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Option.3CIterator.3E.20iterator.html#208173815">(Aug 27 2020 at 02:56)</a>:</h4>
<p>It's hitting <code>unreachable_unchecked</code>. I'm pretty sure that can cause memory unsafety once LLVM goes to town on it</p>



<a name="208173816"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Option%3CIterator%3E%20iterator/near/208173816" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Option.3CIterator.3E.20iterator.html#208173816">(Aug 27 2020 at 02:56)</a>:</h4>
<p>The safety invariant is that <code>Fuse</code> should never set its iterator to <code>None</code> in this case, to keep that <code>unreachable_unchecked</code> valid</p>



<a name="208173840"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Option%3CIterator%3E%20iterator/near/208173840" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Option.3CIterator.3E.20iterator.html#208173840">(Aug 27 2020 at 02:57)</a>:</h4>
<p>Oh you are saying that the <code>Fuse</code> implementation is not setting itself to <code>None</code></p>



<a name="208173846"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Option%3CIterator%3E%20iterator/near/208173846" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Option.3CIterator.3E.20iterator.html#208173846">(Aug 27 2020 at 02:57)</a>:</h4>
<p>Right</p>



<a name="208173867"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Option%3CIterator%3E%20iterator/near/208173867" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Option.3CIterator.3E.20iterator.html#208173867">(Aug 27 2020 at 02:57)</a>:</h4>
<p>ok false alarm</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>